/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chanceit.ssss.fragments;

import java.util.List;

import net.tsz.afinal.http.AjaxCallBack;

import org.gl.GlobalVariable;
import org.gl.customview.EditView;
import org.gl.ui.manger.AjaxCallBackProxy;
import org.gl.utils.AndroidUtil;
import org.gl.utils.StringUtil;
import org.gl.utils.Tip;
import org.gl.utils.WaitThreeSecond;
import org.gl.utils.lg;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.chanceit.ssss.activity.CarTypePickerActivity;
import com.chanceit.ssss.activity.PageViewHandlerI;
import com.chanceit.ssss.activity.R;
import com.chanceit.ssss.common.ActivityActionName;
import com.chanceit.ssss.common.IntentExtraDataKey;
import com.chanceit.ssss.common.ThridApp;
import com.chanceit.ssss.data.DataSources;

//import com.chanceit.ssss.dialog.CarTypeSpinnerDialog;

public class CarerInfoFragment extends BaseFragment implements OnClickListener {
	public final static String ARG_POSITION = "position";
	int mCurrentPosition = -1;
	/**
	 * show all carer info
	 */
	// TextView tv_info;
	/**
	 * content view
	 */
	View view;
	/**
	 * button menu
	 */
	View go_messagecenter;
	View go_project;
	View go_compl;
	View go_orger;
	View iv_user_add;

	ImageView iv_updateInfo;
	Button iv_addInfo;
	/**
	 * define equid ID
	 */
	private int userId;
	/**
	 * go scan
	 */
	private ImageView iv_scan;
	EditView ev_carer_name;
	EditView ev_carer_car_plate;
	EditView ev_carer_car_type;
	EditView ev_carer_tel;
	/**
	 * shenfenzheng
	 */
	EditView ev_carer_identity_card;
	EditView ev_carer_birthday;
	EditView ev_carer_address;
	EditView ev_carer_sendcond_name;
	EditView ev_carer_sendcond_tel;
	EditView ev_carer_car_goucheshijian;
	EditView ev_carer_car_baoxiangongsi;
	EditView ev_carer_car_baoxiandaoqishijian;
	EditView ev_carer_car_nianjianshijian;
	EditView ev_carer_car_run_road;
	TextView carer_info_last_update_time;
	private View ll_carer_info_show;
	private View ll_carer_info_edit;
	private TextView carer_info_btn_more;
	private View carer_info_more_indr;
	private View form_carer_info_btn_more;
	private Context context;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// If activity recreated (such as from screen rotate), restore
		// the previous article selection set by onSaveInstanceState().
		// This is primarily necessary when in the two-pane layout.
		context = getActivity();
		if (savedInstanceState != null) {
			mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
		}
		userId = GlobalVariable.getGlobalInstance().intent.getIntExtra(
				IntentExtraDataKey.USERID, -1);
		view = inflater.inflate(R.layout.carer_info, container, false);
		// tv_info = (TextView) view.findViewById(R.id.tv_carer_info);
		go_messagecenter = view.findViewById(R.id.messagecenter);
		go_project = view.findViewById(R.id.iv_go_project);
		go_compl = view.findViewById(R.id.iv_go_comp);
		go_orger = view.findViewById(R.id.iv_go_order);

		go_compl.setOnClickListener(this);
		go_project.setOnClickListener(this);
		go_orger.setOnClickListener(this);
		go_messagecenter.setOnClickListener(this);
		carer_info_btn_more = (TextView) view
				.findViewById(R.id.carer_info_btn_more);
		carer_info_more_indr = view.findViewById(R.id.carer_info_more_indr);
		carer_info_btn_more.setOnClickListener(this);
		form_carer_info_btn_more = view.findViewById(R.id.from_carer_info_more);
		view.findViewById(R.id.iv_show_user_more).setOnClickListener(this);
		iv_updateInfo = (ImageView) view.findViewById(R.id.iv_user_edit);
		iv_updateInfo.setOnClickListener(this);
		iv_addInfo = (Button) view.findViewById(R.id.iv_user_add);
		iv_addInfo.setOnClickListener(this);
		iv_scan = (ImageView) view.findViewById(R.id.iv_user_scan);
		iv_scan.setOnClickListener(this);
		view.findViewById(R.id.tv_user_indenfer_scan).setOnClickListener(this);

		ll_carer_info_show = view.findViewById(R.id.ll_carer_info_show);
		ll_carer_info_edit = view.findViewById(R.id.scrollView1);
		if (userId < 0) {
			changeState(STATE_ADD);
		} else {
			initData();
			changeState(STATE_SHOW);
		}
		return view;
	}

	final int STATE_ADD = 1;
	final int STATE_SHOW = 0;
	final int STATE_EDIT = 2;
	final int STATE_SHOW_MORE = 3;
	int state = 0;
	public static String isNeedOpenCard = "isAddNewUser";

	void changeState(int state) {
		this.state = state;
		switch (state) {
		case STATE_SHOW:
			iv_addInfo.setVisibility(View.GONE);
			iv_updateInfo.setVisibility(View.VISIBLE);
			ll_carer_info_show.setVisibility(View.VISIBLE);
			ll_carer_info_edit.setVisibility(View.GONE);
			break;
		case STATE_ADD:
			iv_addInfo.setVisibility(View.VISIBLE);
			iv_updateInfo.setVisibility(View.GONE);
			findEditView();
			ll_carer_info_show.setVisibility(View.GONE);
			ll_carer_info_edit.setVisibility(View.VISIBLE);
			break;
		case STATE_EDIT:
			iv_addInfo.setVisibility(View.VISIBLE);
			iv_updateInfo.setImageResource(R.drawable.edit_cancl);
			iv_updateInfo.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					((ImageView) v).setImageResource(R.drawable.edit);
					form_carer_info_btn_more.setVisibility(View.GONE);
					changeState(STATE_SHOW);
					view.findViewById(R.id.scrollView1)
							.setVisibility(View.GONE);
					ll_carer_info_show.setVisibility(View.VISIBLE);
					iv_updateInfo.setOnClickListener(CarerInfoFragment.this);

				}
			});
			findEditView();
			// ll_carer_info_show.setVisibility(View.GONE);
			// ll_carer_info_edit.setVisibility(View.VISIBLE);
			showMoreInfo();
			setAllViewEnable((ViewGroup) (view.findViewById(R.id.scrollView1)),
					true);
			break;
		default:
			break;
		}
	}

	// CarTypeSpinnerDialog mCarTypeSpinnerDialog;
	/**
	 * 选中车型
	 */
	public static final int RequestCode_PickCarType = 0xfff;

	private void findEditView() {

		ev_carer_name = (EditView) view.findViewById(R.id.ev_carer_name);
		ev_carer_car_plate = (EditView) view
				.findViewById(R.id.ev_carer_car_plate);
		ev_carer_car_type = (EditView) view
				.findViewById(R.id.ev_carer_car_type);
		ev_carer_car_type.getEditView().setFocusable(false);
		// if (mCarTypeSpinnerDialog == null) {
		// mCarTypeSpinnerDialog = new CarTypeSpinnerDialog(context,
		// ev_carer_car_type.getEditView());
		// }
		ev_carer_car_type.getEditView().setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = GlobalVariable.getInstance().intent;
						intent.setAction(ActivityActionName.CarTypePickerActivity);
						intent.putExtra(
								CarTypePickerActivity.key_CarBrand,
								GlobalVariable.UserInfo
										.optJSONObject("company").optString(
												"brand"));
						getActivity().startActivityForResult(
								GlobalVariable.getInstance().intent,
								RequestCode_PickCarType);
					}
				});

		ev_carer_tel = (EditView) view.findViewById(R.id.ev_carer_tel);
		ev_carer_identity_card = (EditView) view
				.findViewById(R.id.ev_carer_identity_card);
		ev_carer_birthday = (EditView) view
				.findViewById(R.id.ev_carer_birthday);
		ev_carer_address = (EditView) view.findViewById(R.id.ev_carer_address);
		ev_carer_sendcond_name = (EditView) view
				.findViewById(R.id.ev_carer_sendcond_name);
		ev_carer_sendcond_tel = (EditView) view
				.findViewById(R.id.ev_carer_sendcond_tel);
		ev_carer_car_goucheshijian = (EditView) view
				.findViewById(R.id.ev_carer_car_goucheshijian);
		ev_carer_car_baoxiangongsi = (EditView) view
				.findViewById(R.id.ev_carer_car_baoxiangongsi);
		ev_carer_car_baoxiandaoqishijian = (EditView) view
				.findViewById(R.id.ev_carer_car_baoxiandaoqishijian);
		ev_carer_car_nianjianshijian = (EditView) view
				.findViewById(R.id.ev_carer_car_nianjianshijian);
		ev_carer_car_run_road = (EditView) view
				.findViewById(R.id.ev_carer_car_run_road);
		carer_info_last_update_time = (TextView) view
				.findViewById(R.id.carer_info_last_update_time);
		ev_carer_car_type.setFocusable(true);
		ev_carer_car_type.getEditView().requestFocus();

	}

	@Override
	public void onResume() {
		super.onResume();
		if (ev_carer_name != null
				&& ev_carer_name.getVisibility() == View.VISIBLE) {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					ev_carer_name.setFocusable(true);
					ev_carer_name.getEditView().requestFocus();
				}
			}, 1500);

		}
	}

	private JSONObject Carer_JSONObject;

	private void initData() {
		DataSources ds = new DataSources();

		ds.getCarerInfo(String.valueOf(userId), new AjaxCallBackProxy<String>(
				view.findViewById(R.id.loading)) {

			@Override
			public void onStart() {
				super.onStart();
			}

			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				if (getActivity() == null) {
					return;
				}
				if (TextUtils.isEmpty(t)) {
					loadFailed("result is null");

				} else {
					try {
						JSONObject result = new JSONObject(t);
						Carer_JSONObject = result;
						Context contetxt = getActivity();
						((TextView) view.findViewById(R.id.iv_show_user_sex))
								.setText(contetxt.getString(R.string.sex) + ":"
										+ result.optString("sex"));
						((TextView) view
								.findViewById(R.id.iv_show_user_car_type))
								.setText(contetxt
										.getString(R.string.carer_car_type)
										+ ":"
										+ result.optJSONObject("car")
												.optString("brand")
										+ result.optJSONObject("car")
												.optString("carName"));
						((TextView) view.findViewById(R.id.iv_show_user_tel))
								.setText(contetxt.getString(R.string.carer_tel)
										+ ":" + result.optString("tel"));
						((TextView) view
								.findViewById(R.id.iv_show_user_scan_id))
								.setText(contetxt
										.getString(R.string.carer_car_identity)
										+ ":" + result.optString("identifier"));
						((TextView) view.findViewById(R.id.title_tv_content))
								.setText(result.optString("userName") + "("
										+ result.optString("plate") + ")");
						((TextView) view.findViewById(R.id.title_tv_content))
								.setTextSize(16);
						indentity_new = result.optString("identifier");
						// ImageLoader.getInstance().displayImage(GlobalVariable.URL_image
						// +
						// result.optJSONObject("car").optString("brandImage"),
						// (ImageView)view.findViewById(R.id.iv_user_scan));
						// TODO set value for more info show
					} catch (Exception e) {
						Tip.show("parsed informtion has some errs");
						e.printStackTrace();
						getActivity().onBackPressed();
					}

				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show(strMsg);
			}

		});

	}

	private String indentity_new = null;

	@Override
	public void receiveMessage(Object message) {
		super.receiveMessage(message);
		if (message == null) {
			return;
		}
		Toast.makeText(getActivity(), "scan:" + message, Toast.LENGTH_SHORT)
				.show();
		setIdentity(message);
	}

	private void setIdentity(Object message) {
		if (message==null) {
			Tip.show("设备识别码不能为空");
			return;
		}
		indentity_new = message.toString();
		((TextView) view.findViewById(R.id.tv_user_scan))
				.setText(getResources().getString(R.string.carer_car_identity)
						+ ":" + message);
	}

	@Override
	public void onStart() {
		super.onStart();

		// During startup, check if there are arguments passed to the fragment.
		// onStart is a good place to do this because the layout has already
		// been
		// applied to the fragment at this point so we can safely call the
		// method
		// below that sets the article text.
		Bundle args = getArguments();
		if (args != null) {
			// Set article based on argument passed in
			updateArticleView(args.getInt(ARG_POSITION));
		} else if (mCurrentPosition != -1) {
			// Set article based on saved instance state defined during
			// onCreateView
			updateArticleView(mCurrentPosition);
		}
	}

	public void updateArticleView(int position) {
		// TextView article = (TextView)
		// getActivity().findViewById(R.id.article);
		// article.setText(Ipsum.Articles[position]);
		// mCurrentPosition = position;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		// Save the current article selection in case we need to recreate the
		// fragment
		outState.putInt(ARG_POSITION, mCurrentPosition);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.iv_go_project:
			if (mPageViewHandlerI != null) {// notice activity switch viewpage
											// to project view
				mPageViewHandlerI.switchViewPage(1);
			}
			break;
		case R.id.iv_go_comp:
			startActivity(GlobalVariable.getGlobalInstance().intent.setAction(
					ActivityActionName.MESSAGECENTER).putExtra(
					IntentExtraDataKey.MESSAGE_TYPE,
					IntentExtraDataKey.MESSAGE_TYPE_COM));
			break;
		case R.id.iv_go_order:
			startActivity(GlobalVariable.getGlobalInstance().intent.setAction(
					ActivityActionName.MESSAGECENTER).putExtra(
					IntentExtraDataKey.MESSAGE_TYPE,
					IntentExtraDataKey.MESSAGE_TYPE_ORDER));
			break;
		case R.id.messagecenter:
			startActivity(GlobalVariable.getGlobalInstance().intent.setAction(
					ActivityActionName.MESSAGECENTER).putExtra(
					IntentExtraDataKey.MESSAGE_TYPE,
					IntentExtraDataKey.MESSAGE_TYPE_MESS));
			break;
		case R.id.iv_user_edit:
			changeState(STATE_EDIT);
			break;
		case R.id.iv_user_add:
			updateCarerInfo();
			break;
		case R.id.iv_show_user_more:// show more carer info
			boolean isshowingMoreCarerinfo = form_carer_info_btn_more
					.getVisibility() == View.VISIBLE ? true : false;
			if (isshowingMoreCarerinfo) {
				form_carer_info_btn_more.setVisibility(View.GONE);
				ll_carer_info_show.startAnimation(AnimationUtils.loadAnimation(
						context, R.anim.push_up_in));
				v.setBackgroundResource(R.drawable.sel_more_right);
				changeState(STATE_SHOW);
				form_carer_info_btn_more.startAnimation(AnimationUtils
						.loadAnimation(context, R.anim.fade_out_two_second));
			} else {
				showMoreInfo();
				changeState(STATE_SHOW_MORE);
				v.setBackgroundResource(R.drawable.sel_more_up);
				form_carer_info_btn_more.startAnimation(AnimationUtils
						.loadAnimation(context, R.anim.fade_in_two_second));
			}
			break;
		case R.id.carer_info_btn_more:// show more edit info
			boolean isshowingMore = form_carer_info_btn_more.getVisibility() == View.VISIBLE ? true
					: false;
			if (isshowingMore) {
				form_carer_info_btn_more.setVisibility(View.GONE);
				carer_info_more_indr
						.setBackgroundResource(R.drawable.indr_down_gray);
			} else {
				form_carer_info_btn_more.setVisibility(View.VISIBLE);
				carer_info_more_indr
						.setBackgroundResource(R.drawable.indr_up_gray);
			}
			break;
		case R.id.iv_user_scan:
			// if (!isExistScanApp()) {
			// Toast.makeText(getActivity(),
			// getString(R.string.install_scan_app),
			// Toast.LENGTH_SHORT).show();
			// UpdateManager um = new UpdateManager(getActivity());
			// lg.i(TAG, "install app");
			// um.updateFromAsset("ZXing.apk");
			// Toast.makeText(getActivity(),
			// getString(R.string.please_repeat), Toast.LENGTH_SHORT)
			// .show();
			// }
			// ;
			// try {
			// getActivity()
			// .startActivityForResult(
			// GlobalVariable.getGlobalInstance().intent
			// .setAction(ActivityActionName.SCAN),
			// IntentExtraDataKey.SCAN);
			// } catch (Exception e) {
			// e.printStackTrace();
			// Toast.makeText(getActivity(), "Not found Scan APP",
			// Toast.LENGTH_SHORT).show();
			// }
			// new ThridApp().openThridAppForResult(getActivity(),
			// ActivityActionName.SCAN, "com.google.zxing.client.android",
			// "ZXing.apk", IntentExtraDataKey.SCAN);
			final EditText et = new EditText(getActivity());
			DialogInterface.OnClickListener _listener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case DialogInterface.BUTTON_POSITIVE:
						if (et.getText()==null) {
							dialog.cancel();
						}else{
							lg.i(this, et.getText().toString());
							setIdentity(et.getText().toString());
						}
						
						break;

					default:
						dialog.cancel();
						break;
					}
				}
			};
			new AlertDialog.Builder(getActivity())
					.setView(et)
					.setPositiveButton("是", _listener)
					.setNegativeButton("否", _listener).setTitle("请输入识别码")
					.show();
			break;
		case R.id.tv_user_indenfer_scan:
			new ThridApp().openThridAppForResult(getActivity(),
					ActivityActionName.SCAN, "com.google.zxing.client.android",
					"ZXing.apk", IntentExtraDataKey.SCAN);
			break;

		default:
			break;
		}

	}

	/**
	 * 通过递归 设置一个VIEWGROUP所有的子EditText .setenable
	 * 
	 * @param viewGroup
	 * @param enable
	 */
	void setAllViewEnable(ViewGroup viewGroup, boolean enable) {
		int viewGroupSize = viewGroup.getChildCount();
		for (int i = 0; i < viewGroupSize; i++) {
			View view = viewGroup.getChildAt(i);
			if (view instanceof ViewGroup) {
				setAllViewEnable((ViewGroup) view, enable);
			} else {
				// if (view instanceof EditText) {
				view.setEnabled(enable);
				// }

			}
		}
		ev_carer_car_run_road.getEditView().setEnabled(false);
	}

	private void showMoreInfo() {
		if (Carer_JSONObject != null) {
			JSONObject result = Carer_JSONObject;
			findEditView();
			Context contetxt = getActivity();
			setAllViewEnable((ViewGroup) (view.findViewById(R.id.scrollView1)),
					false);
			onClick(carer_info_btn_more);
			form_carer_info_btn_more.setVisibility(View.VISIBLE);
			view.findViewById(R.id.scrollView1).setVisibility(View.VISIBLE);
			ll_carer_info_show.setVisibility(View.GONE);
			ll_carer_info_show.startAnimation(AnimationUtils.loadAnimation(
					context, R.anim.push_up_out));
			if (result.optString("sex").contains(getString(R.string.man))) {
				((RadioButton) view.findViewById(R.id.carer_info_radio_man))
						.setChecked(true);
			} else {
				((RadioButton) view.findViewById(R.id.carer_info_radio_woman))
						.setChecked(true);
			}

			((TextView) view.findViewById(R.id.iv_show_user_car_type))
					.setText(contetxt.getString(R.string.carer_car_type) + ":"
							+ result.optJSONObject("car").optString("brand")
							+ result.optJSONObject("car").optString("carName"));

			((TextView) view.findViewById(R.id.tv_user_scan)).setText(contetxt
					.getString(R.string.carer_car_identity)
					+ result.optString("identifier"));
			ev_carer_name.setEditText(result.optString("userName"));
			ev_carer_name.getEditView().requestFocus();
			ev_carer_car_type.setEditText(result.optJSONObject("car")
					.optString("carName"));
			ev_carer_car_type.getEditView().setTag(
					result.optJSONObject("car").optInt("carId"));
			ev_carer_car_plate.setEditText(result.optString("plate"));
			ev_carer_identity_card.setEditText(result.optString("phyName"));
			ev_carer_birthday.setEditText(result.optString("birthday"));
			ev_carer_tel.setEditText(result.optString("tel"));

			ev_carer_car_goucheshijian.setEditText(result
					.optString("buycartime"));

			ev_carer_car_baoxiandaoqishijian.setEditText(result
					.optString("insuTime"));
			ev_carer_car_baoxiangongsi.setEditText(result.optString("insuCom"));
			ev_carer_car_nianjianshijian.setEditText(result
					.optString("inspTime"));
			// ev_carer_car_run_road.getEditView().setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
			ev_carer_car_run_road.setEditText(result.optString("mileage"));
			if (!TextUtils.isEmpty(result.optString("recordTime"))
					&& !"null".equals(result.optString("recordTime"))) {
				carer_info_last_update_time.setText("上报时间：\n"
						+ result.optString("recordTime"));
			}
			ev_carer_sendcond_name
					.setEditText(result.optString("nextUserName"));
			ev_carer_sendcond_tel.setEditText(result.optString("nextTel"));
			ev_carer_address.setEditText(result.optString("address"));
			// ImageLoader.getInstance().displayImage(GlobalVariable.URL_image +
			// result.optJSONObject("car").opt("brandImage"),
			// (ImageView)view.findViewById(R.id.iv_show_user_scan));
		}

	}

	/**
	 * is exist the scan app
	 * 
	 * @return
	 */
	private boolean isExistScanApp() {
		PackageManager pm = getActivity().getPackageManager();
		// 查询所有已经安装的应用程序
		List<ApplicationInfo> listAppcations = pm
				.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		// Collections.sort(listAppcations,
		// new ApplicationInfo.DisplayNameComparator(pm));// 排序
		for (ApplicationInfo applicationInfo : listAppcations) {
			if (applicationInfo.packageName
					.equals("com.google.zxing.client.android")) {
				lg.i(TAG, applicationInfo.packageName);
				return true;
			}
		}
		return false;
	}

	private void updateCarerInfo() {
		DataSources ds = new DataSources();
		Integer tempid = null;
		if (userId < 0) {
			tempid = null;
		} else {
			tempid = new Integer(userId);
		}

		// shibiema
		final String identifier = indentity_new;
		if (identifier == null) {
			// identifier = "100010001";// FIXME
			Tip.show(getString(R.string.error_field_required)
					+ getString(R.string.carer_car_identity));
		} else if (!TextUtils.isDigitsOnly(identifier)) {
			Tip.show(getString(R.string.err_format)
					+ getString(R.string.carer_car_identity));
		}

		// username
		String userName = "" + ev_carer_name.getEditView().getText().toString();
		// shenfenzheng
		String phyName = ""
				+ ev_carer_identity_card.getEditView().getText().toString();
		String age = "18";

		if (!TextUtils.isEmpty(phyName)) {
			// 中国身份证长度为18位 最后一位可能为X
			if (phyName.length() != 18) {
				ev_carer_identity_card.getEditView().requestFocus();
				ev_carer_identity_card.getEditView().setError(
						getString(R.string.err_format));
				return;
			}
		}
		if (TextUtils.isEmpty(userName)) {
			ev_carer_name.requestFocus();
			ev_carer_name.getEditView().setError(
					getString(R.string.error_field_required));
			return;
		}

		// plate
		String plate = ""
				+ ev_carer_car_plate.getEditView().getText().toString();
		if (!StringUtil.check(plate, "^[\\u4e00-\\u9faf][\\da-zA-Z]{6}$")) {
			ev_carer_car_plate.getEditView().setError(
					getString(R.string.err_format));
			ev_carer_car_plate.getEditView().requestFocus();
			return;
		}
		// tel
		String tel = "" + ev_carer_tel.getEditView().getText().toString();
		if (TextUtils.isEmpty(tel)) {
			ev_carer_tel.requestFocus();
			ev_carer_tel.getEditView().setError(
					getString(R.string.error_field_required));
			return;
		} else if (!TextUtils.isDigitsOnly(tel)) {
			ev_carer_tel.getEditView().setError(getString(R.string.err_format));
			ev_carer_tel.requestFocus();
			return;
		}

		String sex = ((RadioButton) view
				.findViewById(R.id.carer_info_radio_man)).isChecked() ? getString(R.string.man)
				: getString(R.string.woman);

		String carId = "" + ev_carer_car_type.getEditView().getTag().toString(); // cat
																					// type
																					// id
		String birthday = ""
				+ ev_carer_birthday.getEditView().getText().toString();
		String email = "";
		String maintainMileage = null;
		String maintainTime = null;
		String mileage = null;
		String buycartime = ""
				+ ev_carer_car_goucheshijian.getEditView().getText().toString();
		String insuCom = ""
				+ ev_carer_car_baoxiangongsi.getEditView().getText().toString();
		String inspTme = ""
				+ ev_carer_car_nianjianshijian.getEditView().getText()
						.toString();
		String instTime = ""
				+ ev_carer_car_baoxiandaoqishijian.getEditView().getText()
						.toString();
		String nextUserName = ""
				+ ev_carer_sendcond_name.getEditView().getText().toString();
		String nextTel = ""
				+ ev_carer_sendcond_tel.getEditView().getText().toString();
		String address = ""
				+ ev_carer_address.getEditView().getText().toString();
		ds.updateCarerInfo(tempid, identifier, userName, phyName, age, tel,
				sex, plate, carId, birthday, email, maintainMileage,
				maintainTime, mileage, buycartime, insuCom, instTime, inspTme,
				nextUserName, nextTel, address, new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);

						// iv_updateInfo.setBackgroundDrawable(null);
						// iv_updateInfo
						// .setImageResource(android.R.drawable.ic_menu_edit);
						lg.i(TAG, t);
						int isfinish = 0;
						String message = "";
						try {
							JSONObject j = new JSONObject(t);
							isfinish = j.optInt("code");
							message += j.optString("reason");
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							if (isfinish != 0) {
								loadFailed(message);
								Tip.show(message);
								getActivity().setResult(500);
							} else {
								loadSuccessed();
								state = STATE_SHOW;
								final Intent intent = new Intent();
								intent.putExtra("identifier", identifier);
								if (userId < 0) {// 如果是新增用户
									intent.putExtra(isNeedOpenCard, true);
									DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											switch (which) {
											case DialogInterface.BUTTON_NEGATIVE:
												dialog.cancel();
												getActivity().onBackPressed();
												break;
											case DialogInterface.BUTTON_POSITIVE:
												new ThridApp()
														.openThridApp(
																getActivity(),
																ActivityActionName.OpenCardMainActivity,
																"cn.chanceit.card",
																"CardActivity.apk");
												// getActivity().onBackPressed();
												break;
											default:
												break;
											}
										}
									};
									AndroidUtil.genDialog(getActivity(),
											"为新增用户开通上网卡", "是否开卡", listener,
											listener);
									getActivity().setResult(200, intent);
									Toast.makeText(getActivity(),
											"updateInfo success",
											Toast.LENGTH_SHORT).show();

								} else {// 如果仅仅为编辑用户资料
									intent.putExtra(isNeedOpenCard, false);
									getActivity().setResult(200, intent);
									Toast.makeText(getActivity(),
											"updateInfo success",
											Toast.LENGTH_SHORT).show();
									getActivity().onBackPressed();
								}

							}
						}

					}

					@Override
					public void onStart() {
						super.onStart();
						Toast.makeText(getActivity(), "updateInfo",
								Toast.LENGTH_SHORT).show();
						loading();
						getActivity().setResult(0);
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Toast.makeText(getActivity(), "updateInfo fail",
								Toast.LENGTH_SHORT).show();
						loadFailed(strMsg);
						getActivity().setResult(500);
					}
				});

	}

	PageViewHandlerI mPageViewHandlerI;

	public void setViewPageHandler(PageViewHandlerI context) {
		mPageViewHandlerI = context;

	}

	public void updateCarType(int carid, String name) {
		ev_carer_car_type.getEditView().setTag(carid);
		ev_carer_car_type.getEditView().setText(name);
	}

	@Override
	public boolean onBackPressed() {
		lg.i(TAG, "onBackPressed");
		if (state == STATE_EDIT || state == STATE_ADD) {
			if (!WaitThreeSecond.start) {
				WaitThreeSecond.start();
				Tip.show(getString(R.string.tip_on_edit)
						+ getString(R.string.exit_again_pressed));
				return true;
			} else {
				return false;
			}
		} else if (state == STATE_SHOW_MORE) {
			onClick(view.findViewById(R.id.iv_show_user_more));
			return true;
		}
		return super.onBackPressed();
	}
}